<?php

namespace app\admin\model;

use think\Model;
use think\Db;
use think\facade\Env;
class Content extends Model
{   
    protected $table = 'st_archives';
    protected static function init()
    {
        self::afterInsert(function ($data) {
            $tablename=db('models')->where('id',$data['model_id'])->value('table_name');//附加表名
            $cloumns=array();
            $_tablename=config('database.prefix').$tablename;
            $_columns=Db::query("SHOW COLUMNS FROM {$_tablename}");
            foreach ($_columns as $v) {
                $cloumns[]=$v['Field'];
            }
            $arr=array();
            foreach ($data->toArray() as $k => $v) {
               if (in_array($k,$cloumns)) {
                   $arr[$k]=$v;
               }
            }
            $arr['aid']=$data['id'];
            //附加表图片上传处理
            if ($_FILES) {
               foreach ($_FILES as $kk => $vv) {
                  if ($vv['name']!="") {
                    $arr[$kk]=self::upload($kk);
                  }
               }
            }
           db($tablename)->insert($arr);
        });
        self::afterUpdate(function ($data) {
            
            $tablename=db('models')->where('id',$data['model_id'])->value('table_name');//附加表名
            $cloumns=array();
            $_tablename=config('database.prefix').$tablename;
            $_columns=Db::query("SHOW COLUMNS FROM {$_tablename}");
            foreach ($_columns as $v) {
                $cloumns[]=$v['Field'];
            }
            $arr=array();
            foreach ($data->toArray() as $k => $v) {
               if (in_array($k,$cloumns)) {
                   $arr[$k]=$v;
               }
            }
            $arr['aid']=$data['aid'];
            //附加表图片上传处理
            if ($_FILES) {
               foreach ($_FILES as $kk => $vv) {
                  if ($vv['name']!="") {
                    $arr[$kk]=self::upload($kk);
                  }
               }
            }
            db($tablename)->where('aid',$data['aid'])->update($arr);
        });
        self::afterDelete(function ($data) {
            self::delpic($data['litpic']);//主表图片的删除
            $tablename=db('models')->where('id',$data['model_id'])->value('table_name');//附加表名
            // 类型为附件的字段
            $fields=db('fields')->field('ename')->where('type',6)->where('model_id',$data['model_id'])->select();
            $cloumns=array();
                $_tablename=config('database.prefix').$tablename;
                $_columns=Db::query("SHOW COLUMNS FROM {$_tablename}");
                foreach ($_columns as $v) {
                    $cloumns[]=$v['Field'];
                }
            foreach ($fields as $k => $v) {
                if (in_array($v['ename'],$cloumns)) {
                    $url=db('film')->where('aid',$data['id'])->value($v['ename']);
                    self::delpic($url);//附加表图片的删除
                }
            }
            db($tablename)->where('aid',$data['id'])->delete();//删除附加表内容
        });
    }
    /**
     * 单张图片删除
     */
    public static function delpic($url=""){
        $path=Env::get('root_path')."\\public\\". $url;
        if ($url!==""||!empty($url)) {
            if(file_exists($path)) {
                $res=unlink($path);
                
            }
        }
        
    }
    /**
     * 附加表图片上传
     *
     * @return void
     */
    public static function upload($url){
        $file = request()->file($url);
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move('../public/uploads');
        if ($info) {
          return  "uploads\\".$info->getSaveName();
        }
    }
}
